using System.Collections.Generic;
using UnityEngine;

namespace CinemaDirector
{
	public class ActorItemTrack : ActorTrack
	{
		protected List<Transform> actors;

		protected CinemaActorEvent[] actorEvents;

		protected CinemaActorAction[] actorActions;

		public List<Transform> Actors
		{
			get
			{
				ActorTrackGroup actorTrackGroup = base.TrackGroup as ActorTrackGroup;
				if (actorTrackGroup != null)
				{
					if (actors == null || !Application.isPlaying)
					{
						actors = new List<Transform>();
						actors.Add(actorTrackGroup.Actor);
					}
					return actors;
				}
				MultiActorTrackGroup multiActorTrackGroup = base.TrackGroup as MultiActorTrackGroup;
				if (multiActorTrackGroup != null)
				{
					return multiActorTrackGroup.Actors;
				}
				return null;
			}
		}

		public CinemaActorEvent[] ActorEvents
		{
			get
			{
				if (actorEvents == null || !Application.isPlaying)
				{
					actorEvents = GetComponentsInChildren<CinemaActorEvent>();
				}
				return actorEvents;
			}
		}

		public CinemaActorAction[] ActorActions
		{
			get
			{
				if (actorActions == null || !Application.isPlaying)
				{
					actorActions = GetComponentsInChildren<CinemaActorAction>();
				}
				return actorActions;
			}
		}

		public override void Initialize()
		{
			elapsedTime = -1f;
			CinemaActorEvent[] array = ActorEvents;
			foreach (CinemaActorEvent cinemaActorEvent in array)
			{
				foreach (Transform actor in Actors)
				{
					if (actor != null)
					{
						cinemaActorEvent.Initialize(actor.gameObject);
					}
				}
			}
			CinemaActorAction[] array2 = ActorActions;
			foreach (CinemaActorAction cinemaActorAction in array2)
			{
				cinemaActorAction.Initialize();
			}
		}

		public override void SetTime(float time)
		{
			CinemaActorEvent[] array = ActorEvents;
			foreach (CinemaActorEvent cinemaActorEvent in array)
			{
				float timeTo = time - cinemaActorEvent.Firetime;
				cinemaActorEvent.SetTimeTo(timeTo);
			}
		}

		public override void Pause()
		{
			CinemaActorEvent[] array = ActorEvents;
			foreach (CinemaActorEvent cinemaActorEvent in array)
			{
				cinemaActorEvent.Pause();
			}
		}

		public override void UpdateTrack(float time, float deltaTime)
		{
			float num = elapsedTime;
			elapsedTime = time;
			CinemaActorEvent[] array = ActorEvents;
			foreach (CinemaActorEvent cinemaActorEvent in array)
			{
				if (num <= cinemaActorEvent.Firetime && elapsedTime >= cinemaActorEvent.Firetime)
				{
					foreach (Transform actor in Actors)
					{
						if (actor != null)
						{
							cinemaActorEvent.Trigger(actor.gameObject);
						}
					}
				}
				else
				{
					if (!(num >= cinemaActorEvent.Firetime) || !(elapsedTime < cinemaActorEvent.Firetime))
					{
						continue;
					}
					foreach (Transform actor2 in Actors)
					{
						if (actor2 != null)
						{
							cinemaActorEvent.Reverse(actor2.gameObject);
						}
					}
				}
			}
			CinemaActorAction[] array2 = ActorActions;
			foreach (CinemaActorAction cinemaActorAction in array2)
			{
				if (num <= cinemaActorAction.Firetime && elapsedTime >= cinemaActorAction.Firetime && elapsedTime < cinemaActorAction.Firetime + cinemaActorAction.Duration)
				{
					foreach (Transform actor3 in Actors)
					{
						if (actor3 != null)
						{
							cinemaActorAction.Trigger(actor3.gameObject);
						}
					}
				}
				else if (elapsedTime > cinemaActorAction.Firetime && elapsedTime <= cinemaActorAction.Firetime + cinemaActorAction.Duration)
				{
					foreach (Transform actor4 in Actors)
					{
						if (actor4 != null)
						{
							float time2 = time - cinemaActorAction.Firetime;
							cinemaActorAction.UpdateTime(actor4.gameObject, time2, deltaTime);
						}
					}
				}
				else
				{
					if (!(num <= cinemaActorAction.Firetime + cinemaActorAction.Duration) || !(elapsedTime > cinemaActorAction.Firetime + cinemaActorAction.Duration))
					{
						continue;
					}
					foreach (Transform actor5 in Actors)
					{
						if (actor5 != null)
						{
							cinemaActorAction.End(actor5.gameObject);
						}
					}
				}
			}
		}

		public override void Resume()
		{
			CinemaActorAction[] array = ActorActions;
			foreach (CinemaActorAction cinemaActorAction in array)
			{
				if (!(base.Cutscene.RunningTime > cinemaActorAction.Firetime) || !(base.Cutscene.RunningTime < cinemaActorAction.Firetime + cinemaActorAction.Duration))
				{
					continue;
				}
				foreach (Transform actor in Actors)
				{
					if (actor != null)
					{
						cinemaActorAction.Resume(actor.gameObject);
					}
				}
			}
		}

		public override void Stop()
		{
			elapsedTime = 0f;
			CinemaActorEvent[] array = ActorEvents;
			foreach (CinemaActorEvent cinemaActorEvent in array)
			{
				foreach (Transform actor in Actors)
				{
					if (actor != null)
					{
						cinemaActorEvent.Stop(actor.gameObject);
					}
				}
			}
			CinemaActorAction[] array2 = ActorActions;
			foreach (CinemaActorAction cinemaActorAction in array2)
			{
				foreach (Transform actor2 in Actors)
				{
					if (actor2 != null)
					{
						cinemaActorAction.Stop(actor2.gameObject);
					}
				}
			}
		}
	}
}
